<?php

namespace App\Admin\Controllers;

use App\Models\Card;
use App\Models\Station;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class CardController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '小美卡';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Card);

        $grid->column('id', __('Id'));
        $grid->column('name', '名称');
        $grid->column('price', '价格');
        $grid->column('number', '次数')->display(function () {
            return $this->numberHuman;
        });
        $grid->column('validity_day', '有效期')->display(function () {
            return $this->validityDayHuman;
        });
        $grid->column('image_url', '图片')->image();
        $grid->column('status', '状态')->display(function () {
            return $this->statusText;
        })->label([Card::STATUS_VALID => 'success', Card::STATUS_INVALID => 'danger']);

        $grid->disableFilter();
        $grid->disableExport();
        $grid->disableRowSelector();

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Card::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('name', '名称');
        $show->field('price', '价格');
        $show->field('number', '次数')->as(function () {
            return $this->numberHuman;
        });
        $show->field('status', '状态')->as(function () {
            return $this->statusText;
        });

        $show->field('validity_day', '有效期')->as(function () {
            return $this->validityDayHuman;
        });
        $show->field('image_url', '图片')->image();
        $show->field('careful_item', '注意事项');
        $show->field('mcstation_ids', '适用门店')->as(function () {
            $stations = Station::query()->find($this->mcstation_ids);
            $str = $stations->pluck("name")->implode("，");
            return $str;
        });
        $show->field('created_time', '创建时间');
        $show->field('updated_time', '更新时间');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Card);

        $form->text('name', '名称')->rules('required');
        $form->number('price', '价格')->rules(['required', 'integer', 'min:0'])->help('单位：分');
        $form->text('number', '次数')->rules(['required', 'integer', 'min:-1'])->help('单位：次，-1表示不限次数');
        $form->text('validity_day','有效期')->rules(['required', 'integer', 'min:-1'])->help('单位：天，-1表示永久有效');
        $form->image('image_url', '图片')->uniqueName();
        $form->textarea('careful_item', '注意事项')->rules(['required']);
        $form->listbox('mcstation_ids', '适用门店')->options(Station::pluck('name', 'id'))
            ->settings(['nonSelectedListLabel' => '全部门店', 'selectedListLabel' => '已选择门店']);
        $form->radio('status', '状态')->options(Card::$statusText)->default(Card::STATUS_VALID);

        return $form;
    }
}
